package com.martin.ads.pano360demo;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.martin.ads.vrlib.constant.GLEtc;
import com.martin.ads.vrlib.filters.base.OESFilter;
import com.martin.ads.vrlib.textures.GLOESTexture;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * @author lejun
 * @version V2.1
 * @Company: 深圳市三三得玖通信有限公司
 * @Title: 教视通
 * @Description:
 * @date 17/7/16 09:45
 */
public class CameraActiivty extends AppCompatActivity {

    private SurfaceView surfaceView;
    private Camera mCamera;
    private int viewWidth, viewHeight = 300;//mSurfaceView的宽和高
    SurfaceHolder holder;
    private GLSurfaceView glsv;

    OESFilter oesFilter;

    private int glWidth, glHeight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        surfaceView = (SurfaceView) findViewById(R.id.gl_surface);
        holder = surfaceView.getHolder();
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                viewHeight = height;
                viewWidth = width;
//                viewHeight = 1920;
//                viewWidth = 1080;
                initCamera();

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        });
        initShow();
    }

    /**
     * SurfaceHolder 回调接口方法
     */
    private void initCamera() {
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                mCamera = Camera.open(i);//默认开启后置
            }
        }
        mCamera.setDisplayOrientation(90);//摄像头进行旋转90°
        if (mCamera != null) {
            try {
                Camera.Parameters parameters = mCamera.getParameters();
                //设置预览照片的大小
                parameters.setPreviewFpsRange(viewWidth, viewHeight);
                //设置相机预览照片帧数
                parameters.setPreviewFpsRange(4, 10);
                //设置图片格式
                parameters.setPictureFormat(ImageFormat.JPEG);
                //设置图片的质量
                parameters.set("jpeg-quality", 90);
                //设置照片的大小
                parameters.setPictureSize(viewWidth, viewHeight);
                //通过SurfaceView显示预览
                mCamera.setPreviewDisplay(holder);
                //开始预览
                mCamera.startPreview();
                mCamera.setPreviewCallback(new Camera.PreviewCallback() {
                    @Override
                    public void onPreviewFrame(byte[] data, Camera camera) {
                        ByteBuffer put = ByteBuffer.allocate(data.length)
                                .order(ByteOrder.nativeOrder())
                                .put(data);
                        put.position(0);

                        if (glsv != null) {
                            GLOESTexture glOESTexture = oesFilter.getGlOESTexture();
                            int textureId = glOESTexture.getTextureId();
                            GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
                            GLES20.glBindTexture(GLEtc.GL_TEXTURE_EXTERNAL_OES, textureId);
                            GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, glWidth, glHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, put);

                            glsv.requestRender();
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initShow() {
        glsv = (GLSurfaceView) findViewById(R.id.glsv);
        glsv.setEGLContextClientVersion(2);
        oesFilter = new OESFilter(CameraActiivty.this);
        glsv.setRenderer(new GLSurfaceView.Renderer() {
            @Override
            public void onSurfaceCreated(GL10 gl, EGLConfig config) {
                oesFilter.init();
            }

            @Override
            public void onSurfaceChanged(GL10 gl, int width, int height) {
                glWidth = width;
                glHeight = height;
                oesFilter.onFilterChanged(width, height);
            }

            @Override
            public void onDrawFrame(GL10 gl) {
                oesFilter.onDrawFrame(0);
            }
        });
        glsv.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
}
